package org.sixtynine.stock.service.file;

import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.sixtynine.stock.Constants;
import org.sixtynine.stock.ReturnData;
import org.sixtynine.stock.entity.BaseEntity;

/**
 * @author Thilanka
 * 
 */
public abstract class ExcelProcessor {

	public abstract ReturnData<List<? extends BaseEntity>> process(
			String filePath);

	public Object getCellValue(Cell cell) {
		switch (cell.getCellType()) {
		case Cell.CELL_TYPE_NUMERIC:
			return cell.getNumericCellValue();
		case Cell.CELL_TYPE_STRING:
			return cell.getStringCellValue();
		}

		return null;
	}

	protected boolean validateForNull(Object cellVal) {
		return cellVal != null;
	}

	protected ReturnData<?> validateForText(Object cellVal, String field,
			int row) {
		ReturnData<String> returnData = new ReturnData<String>(Constants.SUCCESS, "");

		if (!validateForNull(cellVal)) {
			returnData.setNo(Constants.ERROR);
			returnData.setMsg(field + " code cannot be null at row " + row);
		}

		if (!(cellVal instanceof String)) {
			returnData.setNo(Constants.ERROR);
			returnData
					.setMsg(field
							+ " code is in the wrong format for some records, please correct them first at row "
							+ row);
		}

		return returnData;
	}
}
